package component;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import pojo.destroy.DestroyBeanByDestructionAwareBeanPostProcessor;

/**
 * @author: ZhouBert
 * @date: 2020/9/21
 * @description:
 */
public class MyDestructionAwareBeanPostProcessor implements DestructionAwareBeanPostProcessor {

	@Override
	public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
		if (bean instanceof DestroyBeanByDestructionAwareBeanPostProcessor){
			DestroyBeanByDestructionAwareBeanPostProcessor destroyBean = (DestroyBeanByDestructionAwareBeanPostProcessor) bean;
			destroyBean.test();
		}
	}

	/**
	 * 只销毁 DestroyBeanByDestructionAwareBeanPostProcessor
	 * @param bean the bean instance to check
	 * @return
	 */
	@Override
	public boolean requiresDestruction(Object bean) {
		if (bean instanceof DestroyBeanByDestructionAwareBeanPostProcessor){
			return true;
		}
		return false;
	}
}
